<template>
  <div class="add">
    <!-- 新增编辑的弹层 -->
    <el-dialog :visible="showDialog" :title="title" :close-on-click-modal="false" @close="isShowDialog">
      <!-- 表单内容 -->
      <el-form ref="form" label-width="100px" :model="form" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item label="权限启用" prop="enVisible">
          <el-switch
            v-model="form.enVisible"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="isShowDialog">取消</el-button>
          <el-button type="primary" @click="btn">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { addPermissionApi, editPermissionApi } from '@/api/permission'
import { Message } from 'element-ui'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      required: true
    },
    id: {
      type: String,
      required: true

    }
  },
  data() {
    return {
      form: {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: ''
      },
      rules: {
        name: [
          { required: true, message: '权限名字不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '权限描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    /**
     * 关闭弹窗
     */
    isShowDialog() {
      this.$emit('update:showDialog', false)
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: ''
      }
    },
    /**
     * 确定提交按钮
     */
    btn() {
      // 1.兜底效验
      this.$refs.form.validate(async key => {
        if (!key) return
        if (this.title === '新增权限-一级') {
        // 2.一级新增权限需要两个必填项
          this.form.type = 1
          this.form.pid = '0'
          // 3.调用接口
          await addPermissionApi(this.form)
          // 4.提示添加成功
          Message.success('添加成功')
        }
        if (this.title === '新增权限-二级') {
        // 2.一级新增权限需要两个必填项
          this.form.type = 2
          this.form.pid = this.id
          // 3.调用接口
          await addPermissionApi(this.form)
          // 4.提示添加成功
          Message.success('添加成功')
        }
        if (this.title === '编辑权限') {
          // 1.调用接口
          await editPermissionApi(this.form)
          // 2.提示成功
          Message.success('编辑成功')
        }
        // 5.关闭弹窗
        this.isShowDialog()
        // 6.渲染页面
        this.$parent.getPermissionList()
      })
    }
  }
}
</script>

<style>

</style>
